// ======================================================================
// Copyright (C) 2013 Hell-Prototypes. / www.hellprototypes.com
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// ======================================================================
#ifndef HELL_ARM_H
#define HELL_ARM_H

#include <QGraphicsScene>
#include <QTableView>
#include <QListWidget>
#include <QStandardItemModel>
#include <QLabel>
#include <QSettings>
#include <QFileDialog>
#include <QTextStream>
#include <QPainter>
#include <QTimer>
#include <QMessageBox>
#include "choose_cfg_file.h"
#include "vision_thread.h"
#include "pap_thread.h"
#include "ctrl_feed_zone.h"
#include "controler_status.h"
#include "define.h"
#include "usb.h"

namespace Ui {
    class Hell_Arm;
}

class Hell_Arm : public QWidget
{
    Q_OBJECT

public:
    explicit Hell_Arm(QWidget *parent = 0);
    ~Hell_Arm();

private:
    Ui::Hell_Arm *ui;

    vision_thread * m_vision_thread;
    pap_thread * m_pap_thread;

    //UI
    QTableView *m_coor_list_view;
    QListWidget *m_value_list_view;
    QStandardItemModel *m_coor_list_model;
    QMap<int, ctrl_feed_zone *> m_ctrl_feed_zone_map;
    controler_status *m_controler_status;

    QSettings *m_config_file;
    QString m_coordinate_list_file;

    choose_cfg_file *m_choose_cfg_file;

    QTimer m_auto_pap_timer;
    QSize m_origin_zone_size;
    double m_origin_zone_area;

    bool m_exe_origin_auto_correct;
    bool m_origin_align_flag;
    bool m_feeder_have_parts;
    bool m_Key_Control_Pressed;

    iPoint m_arm_take_out_pos;

    //Other
    int variant_to_int( const QVariant &var, const int default_value, bool *report = NULL);
    double variant_to_double ( const QVariant &var, const double default_value, bool *report = NULL);
    void load_config();
    void load_feeder_config();
    void store_config();

    //Misc
    void ui_init();
    void coor_list_view_init();
    bool import_coordinate_list_file(QString input_path);
    bool get_next_coordinate(DPoint &next_coordinate);
    bool get_current_part_coordinate(DPoint &curr_coordinate);
    bool get_current_part_parameters(DPoint &curr_coordinate, double &orient);
    bool get_current_part_value(QString &value);
    int get_col_index_from_header(const QString &value);
    QStringList get_col_value_list_by_header(const QString &header);
    bool get_part_feeder_zone_center(iPoint &zone_center);
    void origin_auto_correct(const Point &center_pos);
    bool is_move_ctrl_key ( int key );
    void move_by_ctrl_key ( int key );
    void msdelay(int ms) { m_vision_thread->msdelay(ms); }
    void prepare_for_auto_pap();

public slots:
    void disp_system_msg(QString msg);
    void slot_parts_frame_update(IplImage* frame);
    void slot_debug_image_update(const char * win_name , IplImage* p_bin_image);
    void slot_value_list_itemDoubleClicked ( QListWidgetItem * item );
    void slot_camera_image_size(int w, int h);
    void slot_parts_position_info_process(vector<part_t> * parts_pos);
    void slot_xy_axis_pos(int x, int y);
    void slot_auto_pap_timer_timeout();
    void slot_pap_error_process(int error_code);
    void slot_usb_connected();
    void slot_feeder_zone_set_position(int id);
    void slot_feeder_zone_delete(int id);
    void slot_feeder_move_arm_to_zone(int id);

private slots:
    void on_pb_debug_clicked();
    void on_sb_parts_threshold_valueChanged(int arg);
    void on_cb_parts_realtime_view_clicked(bool checked);
    void on_cb_parts_camera_index_currentIndexChanged(int index);
    void on_pb_arm_move_left_clicked();
    void on_pb_arm_move_right_clicked();
    void on_pb_arm_move_back_clicked();
    void on_pb_arm_move_front_clicked();
    void on_pb_import_coor_file_clicked();
    void on_cb_value_col_filter_currentIndexChanged(const QString &arg1);
    void on_cb_disp_debug_image_clicked(bool checked);
    void on_pb_connect_device_clicked();
    void on_pb_clear_sys_msg_clicked();
    void on_pb_auto_pick_and_place_clicked(bool checked);
    void on_pb_manual_pick_and_place_clicked();
    void on_pb_set_mark_0_clicked();
    void on_pb_set_mark_1_clicked();
    void on_pb_arm_move_to_pcb_part_clicked();
    void on_pb_origin_init_clicked();
    void on_sb_stepmotor_move_speed_valueChanged(int arg1);
    void on_pb_move_arm_to_origin_clicked();
    void on_cb_only_pickup_clicked(bool checked);
    void on_cb_value_col_currentIndexChanged(const QString &value);
    void on_pb_add_feeder_zone_clicked();
    void on_hsb_set_feeder_zone_width_valueChanged(int value);
    void on_hsb_set_feeder_zone_height_valueChanged(int value);
    void on_sb_set_part_area_valueChanged(int value);
    void on_dsb_cam_width_length_valueChanged(double len);
    void on_vsb_pick_up_down_ctrl_valueChanged(int value);
    void on_hsb_picker_angle_ctrl_valueChanged(int value);
    void on_pb_lock_focus_for_move_clicked(bool checked);
    void on_cb_pump_control_clicked(bool checked);
    void on_sb_picker_offset_x_valueChanged(int value);
    void on_sb_picker_offset_y_valueChanged(int value);
    void on_pb_save_cfg_to_file_clicked();
    void on_pb_move_arm_to_take_out_pos_clicked();
    void on_pb_set_arm_take_out_pos_clicked();
    void on_pb_capture_property_setting_clicked();

    void on_hsb_capture_set_brightness_valueChanged(int value);
    void on_hsb_capture_set_contrast_valueChanged(int value);
    void on_hsb_capture_set_saturation_valueChanged(int value);
    void on_hsb_capture_set_hue_valueChanged(int value);
    void on_hsb_capture_set_gain_valueChanged(int value);
    void on_hsb_capture_set_sharpness_valueChanged(int value);
    void on_hsb_capture_set_gamma_valueChanged(int value);
    void on_hsb_capture_set_backlight_valueChanged(int value);
    void on_pb_reset_all_capture_property_clicked();

protected:
    //void resizeEvent( QResizeEvent * event );
    void timerEvent ( QTimerEvent * event );
    void closeEvent ( QCloseEvent * e );
    void focusOutEvent ( QFocusEvent * event );
    void keyPressEvent ( QKeyEvent * event );
    void keyReleaseEvent ( QKeyEvent * event );

    bool nativeEvent(const QByteArray & eventType, void * message, long * result);
};

#endif // HELL_ARM_H
